package com.sk.helper.service;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.sk.helper.data.Stick;
import com.sk.helper.utils.Protocol;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class DeviceService {


    private static final String TAG = "Device---  ";
    private final Handler mHandler;
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private AcceptThread mAcceptThread;
    private ConnectThread mConnectThread;
    public ConnectedThread mConnectedThread;

    public static final int MESSAGE_STATE_CHANGE = 1;
    public static final int MESSAGE_READ = 2;
    public static final int MESSAGE_WRITE = 3;
    public static final int MESSAGE_DEVICE_NAME = 4;
    public static final int MESSAGE_TOAST = 5;


    public static final int STATE_CONNECTED = 3;
    public static final int STATE_CONNECTING = 2;
    public static final int STATE_LISTEN = 1;
    public static final int STATE_NONE = 0;
    private int mState;
    private final BluetoothAdapter mBluetoothAdapter;

    public static final int CMD_KEYEVENT = 1;
    //private BluetoothDevice mBluetoothDevice = null;
    public static int keyState1 = 0;
    public static int temL2 = 0;
    public static int temR2 = 0;
    static int StickLx = 128;
    static int StickLy = 128;
    static int StickRx = 128;
    static int StickRy = 128;
    //MyCallback myCallback;
    public Stick mStick;



    public DeviceService(Context mContext, Handler handler) {
        mHandler = handler;
        mState = STATE_NONE;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(paramString);
        //Log.e(TAG, "device: " + device);
        //start();
        Log.e(TAG, "-----:sssssssssssssssssss ");
        //connect(device);
        //InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
        mStick = new Stick();

    }

   /* public DeviceService(Context mContext, String paramString, Handler handler) {
        mHandler = handler;
        mState = STATE_NONE;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(paramString);
        Log.e(TAG, "device: " + device);
        start();
        Log.e(TAG, "-----:sssssssssssssssssss ");
        connect(device);
        //InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
        mStick = new Stick();

    }*/


    public int switchSpp(int onoff) {
        Log.i("sk---00  switchSpp", "onstart server");
        byte[] paramArrayOfByte = new byte[6];
        paramArrayOfByte[0] = 0x4E;
        paramArrayOfByte[1] = 0x47;
        paramArrayOfByte[2] = 0x01;
        paramArrayOfByte[3] = 0x06;
        paramArrayOfByte[4] = 0x08;

        if (onoff == 1) {

            paramArrayOfByte[5] = 0x01;
            mConnectedThread.write(paramArrayOfByte);
            return 1;
        } else if (onoff == 0) {

            paramArrayOfByte[5] = 0x00;
            mConnectedThread.write(paramArrayOfByte);
            return 0;
        } else {
            return 2;
        }


    }


    public int modifyKeySpp(byte[] paramArrayOfByte) {
        Log.e("sk---00  modifyKeySpp", "onstart");
    //  byte[] paramArrayOfByte = new byte[6];
        for (int i=0;i<paramArrayOfByte.length;i++){
            Log.e("sk---00  switchSpp","i==" +i +"   value=="+(paramArrayOfByte[i]));
        }
        mConnectedThread.write(paramArrayOfByte);
        return 0;
    }

    protected boolean deviceParamValue(byte[] paramArrayOfByte) {
        try {
            if (mState != STATE_CONNECTED) {
                return false;
            }

            return mConnectedThread.sendByteToDevice(paramArrayOfByte);
        } catch (Exception e) {
            Log.e("Device", "deviceParamValue Exception during sendMessage");
        }

        return true;
    }

    private void setState(int paramInt) {
        Log.d("Device",/* this.d + */"  setState() " + mState + " -> " + paramInt);
        mState = paramInt;
    }
    /**
     * Return the current connection state. */
    public synchronized int getState() {
        return mState;
    }
    /**
     * Start the chat service. Specifically start AcceptThread to begin a
     * session in listening (server) mode. Called by the Activity onResume()
     */
    public synchronized void start() {
        Log.e(TAG, "start()---s00---kkk");

        // Cancel any thread attempting to make a connection
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        Log.e(TAG, "start()---s00---222");
        // Start the thread to listen on a BluetoothServerSocket
        if (mAcceptThread == null) {
            mAcceptThread = new AcceptThread();
            mAcceptThread.start();
        }
        Log.e(TAG, "start()---s00---333");
        setState(STATE_LISTEN);
        Log.e(TAG, "start()---s00---444");
    }

    public synchronized void connect(BluetoothDevice device) {
        Log.e(TAG, "connect to: " + device);

        // Cancel any thread attempting to make a connection
        if (mState == STATE_CONNECTING) {
            if (mConnectThread != null) {
                mConnectThread.cancel();
                mConnectThread = null;
            }
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }

        // Start the thread to connect with the given device
        mConnectThread = new ConnectThread(device);
        mConnectThread.start();
        setState(STATE_CONNECTING);
    }

    public void connected(BluetoothSocket paramBluetoothSocket, BluetoothDevice device) {

        Log.d(TAG, " ------connected");


        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }

        mConnectedThread = new ConnectedThread(paramBluetoothSocket);
        mConnectedThread.start();

        setState(STATE_CONNECTED);

        switchSpp(1);
    }


    /**
     * Stop all threads
     */
    public synchronized void stop() {
        Log.e(TAG, "stop");

        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }

        if (mAcceptThread != null) {
            mAcceptThread.cancel();
            mAcceptThread = null;
        }


        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        setState(STATE_NONE);
    }

    /**
     * Write to the ConnectedThread in an unsynchronized manner
     *
     * @param out The bytes to write
     * @see ConnectedThread#write(byte[])
     */
    public void write(byte[] out) {
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED) return;
            r = mConnectedThread;
        }
        // Perform the write unsynchronized
        r.write(out);
    }

    /**
     * Indicate that the connection attempt failed and notify the UI Activity.
     */
    private void connectionFailed() {
        setState(STATE_LISTEN);

    }

    /**
     * Indicate that the connection was lost and notify the UI Activity.
     */
    private void connectionLost() {
        setState(STATE_LISTEN);

    }


    private class AcceptThread extends Thread {
        // The local server socket
        private final BluetoothServerSocket mmServerSocket;

        public AcceptThread() {
            BluetoothServerSocket tmp = null;
            Log.e(TAG, "start()---s00---444444" + mBluetoothAdapter);
            // Create a new listening server socket
            try {
                tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("PadService", MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "listen() failed", e);
            }
            Log.e(TAG, "start()---s00---555" + tmp);
            mmServerSocket = tmp;
        }

        public void run() {
            Log.d(TAG, "BEGIN mAcceptThread" + this);
            setName("AcceptThread");
            BluetoothSocket socket = null;

            // Listen to the server socket if we're not connected
            while (mState != STATE_CONNECTED) {
                try {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    socket = mmServerSocket.accept();
                } catch (IOException e) {
                    Log.e(TAG, "accept() failed", e);
                    break;
                }

                // If a connection was accepted
                if (socket != null) {
                    synchronized (DeviceService.this) {
                        switch (mState) {
                            case STATE_LISTEN:
                            case STATE_CONNECTING:
                                // Situation normal. Start the connected thread.
                                connected(socket, socket.getRemoteDevice());
                                break;
                            case STATE_NONE:
                            case STATE_CONNECTED:
                                // Either not ready or already connected. Terminate new socket.
                                try {
                                    socket.close();
                                } catch (IOException e) {
                                    Log.e(TAG, "Could not close unwanted socket", e);
                                }
                                break;
                        }
                    }
                }
            }
            Log.i(TAG, "END mAcceptThread");
        }

        public void cancel() {
            Log.d(TAG, "cancel " + this);
            try {
                mmServerSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of server failed", e);
            }
        }
    }


    private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;
        private String str;

        public ConnectThread(BluetoothDevice paramBluetoothDevice) {
            mmDevice = paramBluetoothDevice;
            BluetoothSocket tmp = null;
            str = "Secure";
               /* if(paramBoolean)
	            {
	            	str = "Secure";
	            }*/
            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try {
                tmp = paramBluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "create() failed", e);
            }
            mmSocket = tmp;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectThread");
            setName("ConnectThread " + mmDevice.getAddress());

            // Always cancel discovery because it will slow down a connection
            mBluetoothAdapter.cancelDiscovery();

            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mmSocket.connect();
            } catch (IOException e) {
                connectionFailed();
                // Close the socket
                try {
                    mmSocket.close();
                } catch (IOException e2) {
                    Log.e(TAG, "unable to close() socket during connection failure", e2);
                }
                // Start the service over to restart listening mode
                DeviceService.this.start();
                return;
            }

            // Reset the ConnectThread because we're done
            synchronized (DeviceService.this) {
                mConnectThread = null;
            }

            // Start the connected thread
            connected(mmSocket, mmDevice);
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }


    }


    class ConnectedThread extends Thread {

        private static final String TAG = "DeviceConnectedThead---  ";
        public final BluetoothSocket mmConnectedThreadSocket;
        private final InputStream mmConnectedThreadInStream;
        private final OutputStream mmConnectedThreadOutStream;


        public ConnectedThread(BluetoothSocket paramBluetoothSocket) {
            Log.d(TAG, "create ConnectedThread");
            mmConnectedThreadSocket = paramBluetoothSocket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = paramBluetoothSocket.getInputStream();
                tmpOut = paramBluetoothSocket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmConnectedThreadInStream = tmpIn;
            mmConnectedThreadOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] arrayOfByte1 = new byte[4];
            int bytes;
            while (true) {
                try {
                    mmConnectedThreadInStream.read(arrayOfByte1);
                    bytes = arrayOfByte1[3];
                    //Log.i(TAG, "-------sk-1----bytes=="+ bytes);
                    if (bytes <= 0)
                        break;
                    byte[] arrayOfByte2 = new byte[(byte) (bytes - 4)];

                    mmConnectedThreadInStream.read(arrayOfByte2);


                      Log.e(TAG, "-------sk-1----arrayOfByte2=="+ arrayOfByte2);
	  				/*byte[] buffer = new byte[1024];
	 	            //int bytes;
	  				bytes = mmInStream.read(buffer);
	  				//Log.i(TAG, "-------sk-1----bytes=="+ bytes);
	  				for(int i=0;i<bytes;i++)
	  				{
	  					Log.i(TAG, "-------i==" + i +"-buffer--"+ buffer[i]);
	  				}*/
                    //CallbackCommandData(PadDataInfo.getGamePadByte(arrayOfByte1,arrayOfByte2));
                   // Log.e(TAG, "-------sk-1----Byte=="+ getGameByte(arrayOfByte1,arrayOfByte2));
                    CallbackCommandData(getGameByte(arrayOfByte1, arrayOfByte2));
	  			/*	Log.i(TAG, "-------sk--------0000000----");  
	  				PadKeyEvent mPadKeyEvent = new PadKeyEvent(1, 1, 96, 1, 1.0F);
	  				
	  				mIPadCallback.onKeyEvent(mPadKeyEvent);
	  				Log.i(TAG, "-------sk--------111111----");
                    */


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    //e.printStackTrace();
                    //} catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    //e.printStackTrace();
                    break;
                }/*finally {
                    try {
                        Log.e(TAG, "xxxxxxxxxxxxxxxxxx");
                        mmConnectedThreadInStream.close();
                        mmConnectedThreadOutStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }*/

            }
        }

        /**
         * Write to the connected OutStream.
         *
         * @param buffer The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                mmConnectedThreadOutStream.write(buffer);

                // Share the sent message back to the UI Activity
                // mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
                //      .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public boolean sendByteToDevice(byte[] paramArrayOfByte) {
            try {
                mmConnectedThreadOutStream.write(paramArrayOfByte);
                if ((paramArrayOfByte.length > 4) && (((paramArrayOfByte[4] == 9) || (paramArrayOfByte[4] == 10))))
                    // Device.g(this.ConnectedThread).obtainMessage(9).sendToTarget();
                    return true;
            } catch (Exception localException) {
                Log.e("Device", "sendByteToDevice Exception during sendMessage", localException);
            }
            return false;
        }


        public void cancel() {
            try {
                mmConnectedThreadSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }

    }


    public static byte[] getGameByte(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2) {
        byte[] arrayOfByte = new byte[paramArrayOfByte1.length + paramArrayOfByte2.length];
        System.arraycopy(paramArrayOfByte1, 0, arrayOfByte, 0, paramArrayOfByte1.length);
        System.arraycopy(paramArrayOfByte2, 0, arrayOfByte, paramArrayOfByte1.length, paramArrayOfByte2.length);
        return arrayOfByte;
    }

    protected void CallbackCommandData(byte[] paramArrayOfByte) {
        if ((paramArrayOfByte == null))
            return;

		  for(int i=0;i<paramArrayOfByte.length;i++)
		  {
               // Log.i(TAG, "-----read--sk-----paramArrayOfByte--"+ paramArrayOfByte[i]);
		  }

        //Log.i(TAG, "-------sk-----paramArrayOfByte--"+ paramArrayOfByte[4]);
        switch (paramArrayOfByte[4]) {
            case CMD_KEYEVENT:
                //ProtocolsppGamepad localProtocol;
                try {
                    //Log.i(TAG, "-------sk-----000-");
                    //localProtocol = new ProtocolsppGamepad();
                    //Log.i(TAG, "-------sk-----111-");
                    //localProtocol.Protocolsp(paramArrayOfByte);
                    //Log.i(TAG, "-------sk-----222000-");
                    //mIPadCallback.onKeyEvent(localProtocol.mPadKeyEvent[localProtocol.keyCur]);
                    Protocolspp(paramArrayOfByte);
					/*for(int i = 0;i < localProtocol.mListPadKeyEvent.size(); i ++){
						mIPadCallback.onKeyEvent(localProtocol.mListPadKeyEvent.get(i));
					}

					//Log.i(TAG, "-------sk-----00011-");
					for(int j = 0;j < localProtocol.mListPadMotionEvent.size(); j ++){
						mIPadCallback.onMotionEvent(localProtocol.mListPadMotionEvent.get(j));
					}*/

		    		    	/*if(localProtocol.keyCur>0)
		    		    	{
		    		    		localProtocol.keyCur--;
		    		    		if(localProtocol.keyCount>0)
		    		    		localProtocol.keyCount--;
		    		    	}*/
                    //Log.i(TAG, "-------sk-----333-");
                } catch (Exception localException) {
                    localException.printStackTrace();
                    return;
                }

                break;
        }

    }


    public void Protocolspp(byte[] paramArrayOfByte) {

        // TODO Auto-generated constructor stub
        // Log.d("Protocol", mListPadKeyEvent.a(paramArrayOfByte));
        //Log.e("Protocolsp", "i==ssssssssssssssssssl21="+paramArrayOfByte[13]);
        //Log.d("Protocolsp", "i==ssssssssssssssssssr2="+paramArrayOfByte[14]);
        int temp = 0;
        int tem2 = 0;
        temp = (paramArrayOfByte[5] & 0xff) + ((paramArrayOfByte[6] & 0x03) << 8);

        for (int j = 0; j < 6; j++) {
            if ((paramArrayOfByte[7 + j] & 0xFF) == 255)
                temp += (0x01 << (j + 10));
        }

        try {

            for (int i = 0; i < 16; i++) {
                //if(i>15)
                //mListPadKeyEvent.add(new PadKeyEvent(1, 1, arrayList[i],KEYPRESSURE,LR_Pressure(paramArrayOfByte[i-3])));
                //else
                {
                    tem2 = 0x01 << i;
                    //Log.e("Protocol down--", "keyState1=="+keyState1 + "tem2 ="+tem2);
                    if ((temp & tem2) == tem2) {
                        if ((keyState1 & tem2) == 0x00) {
                            Log.e("Protocol down--", "i==" + Protocol.arrayList[i]);
                            keyState1 |= tem2;
                            //mListPadKeyEvent.add(new PadKeyEvent(1, 1, arrayList[i],KEYDOWN, 1.0F));
                            //	Instrumentation m_Instrumentation = new Instrumentation();
                            //	m_Instrumentation.sendKeyDownUpSync(KEYCODE_BUTTON_A);

                            mHandler.obtainMessage(Protocol.MESSAGE_KEY, Protocol.KEYDOWN, -1, Protocol.arrayList[i]).sendToTarget();
                            //myCallback.PadKeyEvent(Protocol.arrayList[i],Protocol.KEYDOWN,-1);

                        }
                    } else {
                        if ((keyState1 & tem2) == tem2) {
                            Log.e("Protocol up--", "i==" + Protocol.arrayList[i]);
                            keyState1 &= (~tem2);
                            //mListPadKeyEvent.add(new PadKeyEvent(1, 1, arrayList[i],KEYUP, 1.0F));
                            mHandler.obtainMessage(Protocol.MESSAGE_KEY, Protocol.KEYUP, -1, Protocol.arrayList[i]).sendToTarget();
                            //myCallback.PadKeyEvent(Protocol.arrayList[i],Protocol.KEYUP,-1);
                        }
                    }
                }

            }

            while (true) {
                //Log.e("Protocol sskkk-", "paramArrayOfByte[13]="+paramArrayOfByte[13]+"  temL2="+temL2);
               // Log.e("Protocol sskkk-", "paramArrayOfByte[13]==" + LR_Pressure(paramArrayOfByte[13] & 0xFF));
                //Log.e("Protocol sskkk-", "paramArrayOfByte[14]==" + LR_Pressure(paramArrayOfByte[14] & 0xFF));
                if ((paramArrayOfByte[13] & 0xFF) != temL2) {
                    //	mListPadKeyEvent.add(new PadKeyEvent(1, 1, arrayList[16],KEYPRESSURE,LR_Pressure(paramArrayOfByte[13]&0xFF)));
                    //mHandler.obtainMessage(Protocol.MESSAGE_LR,Protocol.arrayList[16],-1,11).sendToTarget();
                    if ((paramArrayOfByte[13] & 0xFF) >= 255) {
                        mHandler.obtainMessage(Protocol.MESSAGE_KEY, Protocol.KEYDOWN, -1, Protocol.arrayList[16]).sendToTarget();
                    } else {
                        mHandler.obtainMessage(Protocol.MESSAGE_KEY, Protocol.KEYUP, -1, Protocol.arrayList[16]).sendToTarget();
                    }
                    //mHandler.obtainMessage(Protocol.MESSAGE_LR,Protocol.arrayList[16],-1,LR_Pressure(paramArrayOfByte[13]&0xFF)).sendToTarget();
                    //LR_Pressure(paramArrayOfByte[13]&0xFF);

                    //myCallback.PadMotionEvent(Protocol.arrayList[16],LR_Pressure(paramArrayOfByte[13]&0xFF),0);

                    //myCallback.PadMotionEvent(Protocol.arrayList[16],LR_Pressure(paramArrayOfByte[13]&0xFF),0);

                }
                if ((paramArrayOfByte[14] & 0xFF) != temR2) {
                    if ((paramArrayOfByte[14] & 0xFF) >= 255) {
                        mHandler.obtainMessage(Protocol.MESSAGE_KEY, Protocol.KEYDOWN, -1, Protocol.arrayList[17]).sendToTarget();
                    } else {
                        mHandler.obtainMessage(Protocol.MESSAGE_KEY, Protocol.KEYUP, -1, Protocol.arrayList[17]).sendToTarget();
                    }
                    //mListPadKeyEvent.add(new PadKeyEvent(1, 1, arrayList[17],KEYPRESSURE,LR_Pressure(paramArrayOfByte[14]&0xFF)));
                }


                if (((paramArrayOfByte[15] & 0xFF) != StickLx) || ((paramArrayOfByte[17] & 0xFF) != StickLy)) {
                    //Log.d("P ssk1--", "xxxx--"+(paramArrayOfByte[15]&0xFF)+
                    //"  yy--="+(paramArrayOfByte[17]&0xFF));
                    //	mListPadMotionEvent.add(new PadMotionEvent(1, 1, arrayList[18],
                    //			ControllerStickLx(paramArrayOfByte[15]&0xFF),
                    //			ControllerStickLy(paramArrayOfByte[17]&0xFF)));

                    mStick.Stickx = ControllerStickLx(paramArrayOfByte[15] & 0xFF);
                    mStick.Sticky = ControllerStickLy(paramArrayOfByte[17] & 0xFF);


                    Log.e("P ssk1--", "xxxx--"+mStick.Stickx + "yyy--"+mStick.Sticky);
                    mHandler.obtainMessage(Protocol.MESSAGE_LR, Protocol.arrayList[18], -1, mStick).sendToTarget();

                }
                //if (((paramArrayOfByte[19]&0xFF) != StickRx) || ((paramArrayOfByte[21]&0xFF) != StickRy))
                {
                    //Log.d("P ssk1--", "xxxx--"+(paramArrayOfByte[19]&0xFF)+ "xxxx+++"+
                    //	"  yy--="+(paramArrayOfByte[21]&0xFF)+"  arrayList19++="+arrayList[19]);
                    //mListPadMotionEvent.add(new PadMotionEvent(1, 1, arrayList[19],
                    //		ControllerStickRx(paramArrayOfByte[19]&0xFF),
                    //		ControllerStickRy(paramArrayOfByte[21]&0xFF)));
                }
                temL2 = paramArrayOfByte[13] & 0xFF;
                temR2 = paramArrayOfByte[14] & 0xFF;

                //StickLx = paramArrayOfByte[15]&0xFF;
                //StickLy = paramArrayOfByte[17]&0xFF;
                //StickRx = paramArrayOfByte[19]&0xFF;
                //StickRy = paramArrayOfByte[21]&0xFF;

                return;


            }
            //if(paramArrayOfByte[14]!=0)
            //mListPadKeyEvent.add(new PadKeyEvent(1, 1, arrayList[17],KEYPRESSURE,LR_Pressure(paramArrayOfByte[14])));

            //	mListPadMotionEvent.add(new PadMotionEvent(1, 1, paramInt1, a(paramInt2, false), a(paramInt3, true)));

        } catch (Exception localException) {
            localException.printStackTrace();
        }

    }


    public float LR_Pressure(int paramInt) {

        if (paramInt <= 0)
            return 0.0F;
        if (paramInt >= 255)
            return 1.0F;
        return 1.0F * paramInt / 255.0F;
    }

    private float ControllerStickLx(int paramInt) {
        float f1 = 1.0F;
        if (paramInt == 128) {
            f1 = 0.0F;
            return f1;
        }
	  /*  if (paramInt <128)
	    {
	     // f1 = -1.0F;
	      return -1.0F;
	    }
	    if (paramInt >128)
	    {
	     // f1 = -1.0F;
	      return 1.0F;
	    }*/
        //while (true)
        {
            //if (paramBoolean);
            //return -f1;
            if (paramInt >= 255)
                return 1.0F;
            //continue;
            //if (paramInt > 128)
            f1 = f1 * (paramInt - 128) / 128.0F;
            // else
            // f1 = 0-(f1 * (  128-paramInt) / 128.0F);
            Log.d("f1===", "f1--=" + f1);
            // else
            //f1 = f1 * paramInt / 128.0F;
            return f1;
        }

    }

    private float ControllerStickLy(int paramInt) {
        float f1 = 1.0F;
        if (paramInt == 128) {
            f1 = 0.0F;
            return f1;
        }

        paramInt = 255 - paramInt;
		   /* if (paramInt <128)
		    {
		     // f1 = -1.0F;
		      return 1.0F;
		    }
		    if (paramInt >128)
		    {
		     // f1 = -1.0F;
		      return -1.0F;
		    }*/
        //while (true)
        {
            //if (paramBoolean);
            //return -f1;
            if (paramInt >= 255)
                return 1.0F;
            //continue;
            // if (paramInt > 128)
            f1 = f1 * (paramInt - 128) / 128.0F;
            //else
            //f1 = 0-(f1 * (  128-paramInt) / 128.0F);
            // Log.d("f2===", "f2--="+f1);
            // else
            //f1 = f1 * paramInt / 128.0F;
            return f1;
        }

    }

    private float ControllerStickRx(int paramInt) {
        float f1 = 1.0F;
        if (paramInt == 128) {
            f1 = 0.0F;
            return f1;
        }

		    /*if (paramInt <128)
		    {
		     // f1 = -1.0F;
		      return -1.0F;
		    }
		    if (paramInt >128)
		    {
		     // f1 = -1.0F;
		      return 1.0F;
		    }*/
        //while (true)
        {
            //if (paramBoolean);
            //return -f1;
            if (paramInt >= 255)
                return 1.0F;
            //continue;
            //if (paramInt > 128)
            f1 = f1 * (paramInt - 128) / 128.0F;
            //else
            // f1 = 0-(f1 * (  128-paramInt) / 128.0F);
            // Log.d("f1===", "f1--="+f1);
            // else
            //f1 = f1 * paramInt / 128.0F;
            return f1;
        }

    }

    private float ControllerStickRy(int paramInt) {
        float f1 = 1.0F;
        if (paramInt == 128) {
            f1 = 0.0F;
            return f1;
        }
        paramInt = 255 - paramInt;
	   /* if (paramInt <128)
	    {
	     // f1 = -1.0F;
	      return -1.0F;
	    }
	    if (paramInt >128)
	    {
	     // f1 = -1.0F;
	      return 1.0F;
	    }*/
        // f1 = -1.0F;
        //while (true)
        {
            //if (paramBoolean);
            //return -f1;
            if (paramInt >= 255)
                return 1.0F;
            //continue;
            //if (paramInt > 128)
            f1 = f1 * (paramInt - 128) / 128.0F;
            // else
            //f1 = f1 * paramInt / 128.0F;
            return f1;
        }

    }

    //public void setListener(MyCallback Callback)
//	{
//		myCallback=Callback;
//	}

}
